<?php
namespace app\admin\controller;

/**
 * Created by PhpStorm.
 * User: zjw
 * Date: 2019/10/10
 * Time: 21:57

 大客户企业类型
 */

use app\common\model\AfficheTypeModel;
use think\Lang;

class Memberdatype extends AdminControl
{
    protected $afficheType;

     /**
     * @description:
     */
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/affiche.lang.php');
    }

    public function index()
    {
        $brand_model = model('enterprise');
        /**
         * 检索条件
         */
        if (!empty(input('param.search_brand_name'))) {
            $condition['title'] = array('like', "%" . input('param.search_brand_name') . "%");
        }
        $condition['id'] = ['gt',0];
        $condition['is_del'] = 0;

        $list = $brand_model->getDaEnterpriseTypeList($condition, "*", 10);

        $this->assign('list', $list);
        $this->assign('search_brand_name', trim(input('param.search_brand_name')));
       $this->assign('showpage', $brand_model->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch('index');

        // if (!empty(input('param.search_brand_name'))) {
        //     $condition['name'] = array('like', "%" . input('param.search_brand_name') . "%");
        // }
        // $condition['id'] = ['gt',0];
        // $condition['enterprisetype_del'] = 0;
        // $list = db('enterprisetype')->where($condition)->select();
        // $this->assign('list', $list);
        // $this->assign('search_brand_name', trim(input('param.search_brand_name')));
        // $this->setAdminCurItem('index');
        // return $this->fetch('index');
    }

    public function add()
    {
        $brand_array = [
            'status' => 1
        ];
        $this->assign('brand_array',$brand_array);
        $this->setAdminCurItem('add');
        $brand_status = [
            'status' => 1
        ];
        $this->assign('enterprisetype_edit',$brand_status);
        return $this->fetch('add');
    }

    /**
     * @description: 编辑 @MrLiu
     */
    public function edit()
    {
        $id = intval(input('param.id'));

        $data = db('member_da_type')->where(['id'=>$id])->find();
        $this->assign('data', $data);
        $brand_array = [
            'status' => $data['status']
        ];
        $this->assign('brand_array',$brand_array);
        
        $brand_status = [
            'status' => 2
        ];
        $this->assign('enterprisetype_edit',$brand_status);
        $this->setAdminCurItem('edit');
        return $this->fetch('add');
    }

    /**
     * @description: 更新或者保存 @MrLiu
     */
    public function save()
    {
        $id = intval(input('param.id'));
        if(empty($id)){
            //新增
            $data = array(
                'title' =>   input('param.name'),
                'status' =>   input('param.status'),
                'create_time' => time(),
                'sort' => input('param.sort'),
             );
            $status = db('member_da_type')->insertGetId($data);
            if ($status>=0) {
                dsLayerOpenSuccess('新增成功');
            } else {
                $this->error('新增失败');
            }
        }else{
            //编辑
            $data = array(
                'title' =>   input('param.name'),
                'status' =>   input('param.status'),
                'sort' => input('param.sort'),
            );
            $status = db('member_da_type')->where(['id'=>$id])->update($data);
            if ($status>=0) {
                dsLayerOpenSuccess('编辑成功');
            } else {
                $this->error('编辑失败');
            }

        }

    }
    /*
     * 上下架
     */

    public function status()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));
        if (empty($id)) {
            ds_json_encode(10001, '参数错误');
        }
        if($status == 1){
            $tag = db('member_da_type')->where('status','1')->count();
            if($tag == 1){
                ds_json_encode(10001, '必须保留一个企业类型是商家状态');
            }
            $tag = db('member')->where('memberda_type_id',$id)->find();
            if(!empty($tag)){
                ds_json_encode(10001, '不能禁用：该类型已绑定大客户');
            }
        }
        $data = array();
        if($status == 1){
            $data['status'] = 2;
        }else{
            $data['status'] = 1;
        }
        $rs = db('member_da_type')->where('id', $id)->data($data)->update();
        if ($rs) {
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }

    /**
     * @description: 删除操作  @MrLiu
     */
    public function cancels()
    {
        $id = intval(input('param.id'));

        $res = db('member_da_type')->where(['id'=>$id])->find();
        if($res['status'] == 1){
            ds_json_encode(10001, '请先禁用之后再删除');
        }
        $tag = db('member_da_type')->select();
        if(count($tag) == 1){
            ds_json_encode(10001, '必须保留一个企业类型');
        }
        $status = db('member_da_type')->where(['id'=>$id])->update(['is_del'=>1,'del_time'=>time()]);
        if ($status === false) {
            ds_json_encode(10001, '删除失败');
        }
//        $this->log(lang('ds_add') . lang('affiche_type_index_class') . '[ID:' . $id . ']', 1);
        ds_json_encode(10000, '删除成功');
    }

    /**
     * @description: 检车名称是否存在  @MrLiu
     * @return bool
     */
    protected function check_type_name($name)
    {
        $afficheTypeModel = new AfficheTypeModel();
        $data = $afficheTypeModel->getOneAfficheType(array('name' => $name));
        if (!empty($data)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * @description: 检车名称是否存在  @MrLiu
     * @return bool
     */
    protected function check_name($name)
    {
        var_dump($name);exit;
        $afficheTypeModel = new AfficheTypeModel();
        $data = $afficheTypeModel->getOneAfficheType(array('name' => $name));
        if (!empty($data)) {
            return true;
        } else {
            return false;
        }
    }
    public function ajax() {
        $adv_model = model('adv');
        switch (input('get.branch')) {
            case 'ap_branch':
                $column = trim(input('param.column'));
                $value = trim(input('param.value'));
                $adv_id = intval(input('param.id'));
                $param['ap_id'] = $adv_id;
                $param[$column] = trim($value);
                $result = $adv_model->editAdvposition($param);
                break;
            //ADV数据表更新
            case 'adv_branch':
                $column = trim(input('param.column'));
                $value = trim(input('param.value'));
                $adv_id = intval(input('param.id'));
                $param[$column] = trim($value);

                $result = $adv_model->editDaEnterprisetype($param, $adv_id);
                break;
        }
        if($result>=0){
            echo 'true';
        }else{
            echo false;
        }
    }

    /**
     * @description:   @MrLiu
     * @return array
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_manage'),
                'url' => url('Memberdatype/index')
            ),
        );

        if (request()->action() == 'add' || request()->action() == 'index') {
            $menu_array[] = [
                'name' => 'add',
                'text' => lang('ds_new'),
                'url' => "javascript:dsLayerOpen('" . url('Memberdatype/add') . "','" . '新增' . "')",
            ];
        }

        if (request()->action() == 'edit') {
            $menu_array[] = array(
                'name' => 'edit',
                'text' => lang('ds_edit'),
                'url' => 'javascript:void(0)'
            );
        }
        return $menu_array;
    }
}